package slowka;

import java.util.ResourceBundle;
import javax.swing.table.*;

public class SuitePanel extends javax.swing.JPanel {
    private ResourceBundle resourceBundle;
    /** Creates new form SuitePanel
     * @param n name of Suite
     * @param s suite displayed in this panel
     */
    public SuitePanel(String n, Suite s, ResourceBundle rb) {
        name = n;//martusia lubi szymusia
        wordsSuite = s;
        resourceBundle = rb;
        initComponents();
        wordsSuite.WriteToTable(WordsTable);
    }
    /** Creates panel for suite
     * @param s suite displayed in this panel
     */
    public SuitePanel(Suite s, ResourceBundle rb) {
        this(s.getName(), s, rb);
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        DeleteWordButton = new javax.swing.JButton();
        DiscardWordsButton = new javax.swing.JButton();
        NewWordButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        WordsTable = new javax.swing.JTable();

        DeleteWordButton.setText(resourceBundle.getString("DELETE"));
        DeleteWordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteWordButtonActionPerformed(evt);
            }
        });

        DiscardWordsButton.setText(resourceBundle.getString("DISCARD"));
        DiscardWordsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DiscardWordsButtonActionPerformed(evt);
            }
        });

        NewWordButton.setText(resourceBundle.getString("ADD"));
        NewWordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NewWordButtonActionPerformed(evt);
            }
        });

        WordsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}, {null, null}
            },
            new String [] {
                resourceBundle.getString("ENGLISH"), resourceBundle.getString("POLISH")
            }
        ));
        WordsTable.setPreferredSize(null);
        jScrollPane1.setViewportView(WordsTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(NewWordButton, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(DeleteWordButton, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DiscardWordsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NewWordButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(DeleteWordButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(DiscardWordsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }//GEN-END:initComponents

    private void NewWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NewWordButtonActionPerformed
        DefaultTableModel TM = (DefaultTableModel) WordsTable.getModel();
        TM.addRow(new Object[] {"", ""});
        WordsTable.setModel(TM);
}//GEN-LAST:event_NewWordButtonActionPerformed
    private void DeleteWordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteWordButtonActionPerformed
        DefaultTableModel TM = (DefaultTableModel) WordsTable.getModel();
        int[] toDelete = WordsTable.getSelectedRows();

        for(int i = toDelete.length-1; i >= 0; i--)
            TM.removeRow(toDelete[i]);

        WordsTable.setModel(TM);
}//GEN-LAST:event_DeleteWordButtonActionPerformed
    private void DiscardWordsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DiscardWordsButtonActionPerformed
        wordsSuite.WriteToTable(WordsTable);
}//GEN-LAST:event_DiscardWordsButtonActionPerformed

    public Suite getWordsSuite() {
        return wordsSuite;
    }
    @Override
    public String getName() {
        return resourceBundle.getString(name);
    }
    /** saves data from table to Suite object */
    void loadFromTable() {
        wordsSuite = new Suite(name, WordsTable);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DeleteWordButton;
    private javax.swing.JButton DiscardWordsButton;
    private javax.swing.JButton NewWordButton;
    private javax.swing.JTable WordsTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private String name;
    private Suite wordsSuite;
}
